package com.x.chalk.service.impl;

import com.x.chalk.dao.SignTaskDao;
import com.x.chalk.dao.UserDao;
import com.x.chalk.dto.SignTaskDto;
import com.x.chalk.service.intf.SignTaskService;
import com.x.chalk.vo.R;
import org.springframework.stereotype.Service;

@Service
public class SignTaskImpl implements SignTaskService {
    private final SignTaskDao dao;
    private final UserDao userDao;

    public SignTaskImpl(SignTaskDao dao, UserDao userDao) {
        this.dao = dao;
        this.userDao = userDao;
    }

    @Override
    public R getScore(Long taskid,String token) {
        Long uid = Long.parseLong(token);
        if((int)queryFlag(token,taskid).getData() == 2){
            int score = dao.selectScore(taskid);
            if (score != 0){
                userDao.updateScore(uid,score);
                dao.updateFlag(3,uid,taskid);
                return R.ok("任务完成，添加积分" + score);
            }
        }else if((int)queryFlag(token,taskid).getData() == 3){
            return R.fail("您的积分已领取，请勿重复领取");
        }else{
            return R.fail("您的任务还未完成！");
        }

        return null;
    }

    @Override
    public R updateFlag(SignTaskDto dto, String token) {
        if(dto != null){
            Long uid = Long.parseLong(token);
            return R.ok( dao.updateFlag(dto.getFlag(),uid, dto.getTaskid()));
        }
        return R.fail();
    }

    @Override
    public R queryFlag(String token,Long taskid) {
        return R.ok(dao.queryFlag(Long.parseLong(token),taskid));
    }
}
